﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using System.Globalization;

namespace University.Workshop.Business
{
    public static class ProductRecommendationManager
    {
        public static Collection<Product> SearchProductRecommendation(String[] words, string category, long productId)
        {
            Collection<Product> allProducts = new Collection<Product>();
            Collection<Product> resp = new Collection<Product>();
            allProducts = GetProductByCategory(category,productId);
            if (allProducts != null && allProducts.Count != 0)
            {
                allProducts = GetSearchProducts(words, allProducts);
                resp = DropRepeatedValues(allProducts);


            }
            else
            {
                throw new BusinessException("No products match with a similar name and category!!");
            }
            return resp;

        }
        public static Collection<Product> GetProductByCategory(string category, long productId)
        {
            Collection<Product> products = new Collection<Product>();
            products = (Collection<Product>)ProductDataAccess.ViewRecommendedProductsOnSale(productId, category);
            return products;
        }
        public static Collection<Product> GetSearchProducts(String[] words, Collection<Product> products)
        {
            Collection<Product> resp = new Collection<Product>();
            if (products != null && products.Count != 0)
            {
                if (words != null && words.Length != 0)
                {
                    foreach (String word in words)
                    {
                        foreach (Product prod in products)
                        {
                            String name = prod.Name.ToLower(CultureInfo.CurrentCulture);
                            String lword = word.ToLower(CultureInfo.CurrentCulture);
                            if (!String.IsNullOrEmpty(word) && name.Contains(lword))
                            {
                                resp.Add(prod);
                            }
                        }
                    }
                }
            }
            return resp;
        }
        public static Collection<Product> DropRepeatedValues(Collection<Product> repeatedValues)
        {
            Collection<Product> noRepeatedValues = new Collection<Product>();
            int j = 0, i;
            if (repeatedValues != null && repeatedValues.Count != 0)
            {
                foreach (Product prod1 in repeatedValues)
                {
                    i = 0;
                    if (j == 0)
                    {
                        noRepeatedValues.Add(prod1);
                    }
                    foreach (Product prod2 in noRepeatedValues)
                    {
                        if (prod1.ProductId != prod2.ProductId)
                        {
                            if (i == noRepeatedValues.Count - 1)
                            {
                                noRepeatedValues.Add(prod1);
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                        i++;
                    }
                    j++;
                }
            }
            return noRepeatedValues;
        }



    }
}
